package cn.caishen.report.service.report.impl;

import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.dao.report.ReportWhereDao;
import cn.caishen.report.domain.report.ReportWhere;
import cn.caishen.report.service.report.ReportWhereService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 报表条件配置Service实现类
 *
 * @author LB
 * @description 报表条件配置Service实现类
 * @create 2022-01-17 15:55:40
 */
@Service("reportWhereService")
@Transactional(rollbackFor = Throwable.class)
public class ReportWhereServiceImpl extends ServiceImpl<ReportWhereDao, ReportWhere> implements ReportWhereService {

    private final ReportWhereDao reportWhereDao;

    public ReportWhereServiceImpl(ReportWhereDao reportWhereDao) {
        this.reportWhereDao = reportWhereDao;
    }

    /**
     * 分页查询列表
     *
     * @param page  页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo 分页列表
     */
    @Override
    public PageInfo<ReportWhere> getList(int page, int limit, LbMap param) {
        QueryWrapper<ReportWhere> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page, limit);
        List<ReportWhere> list = reportWhereDao.selectList(wrapper);
        return new PageInfo<ReportWhere>(list);
    }

    /**
     * 查询所有列表
     *
     * @param param 参数
     * @return list 全部列表
     */
    @Override
    public List<ReportWhere> getAllList(LbMap param) {
        QueryWrapper<ReportWhere> wrapper = new QueryWrapper<>();
        return reportWhereDao.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return 报表条件配置
     */
    @Override
    @SneakyThrows
    public ReportWhere getById(String primaryKey) {
        ReportWhere reportWhere = reportWhereDao.selectById(primaryKey);
        if (Objects.isNull(reportWhere)) {
            throw new BusinessException("报表条件配置编号不正确");
        }
        return reportWhere;
    }

    /**
     * 新增信息
     *
     * @param reportWhere reportWhere
     */
    @Override
    @SneakyThrows
    public void saveOne(ReportWhere reportWhere) {
        int count = reportWhereDao.insert(reportWhere);
        if (count != 1){
            throw new BusinessException("报表条件配置保存失败");
        }
    }

    /**
     * 更新信息
     *
     * @param reportWhere reportWhere
     */
    @Override
    @SneakyThrows
    public void update(ReportWhere reportWhere) {
        int count = reportWhereDao.updateById(reportWhere);
        if (count != 1){
            throw new BusinessException("报表条件配置更新失败");
        }
    }

    /**
     * 删除信息
     * 
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        ReportWhere reportWhere = reportWhereDao.selectById(primaryKey);
        if (Objects.isNull(reportWhere)) {
            throw new BusinessException("未通过编号找到报表条件配置");
        }
        int count = reportWhereDao.deleteById(primaryKey);
        if (count != 1){
            throw new BusinessException("报表条件配置删除失败");
        }
    }
}
